package com.newer.weatherforecast.xml;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.newer.weatherforecast.entity.CurrentWeather;
import com.newer.weatherforecast.entity.ForecastWeather;


public class CnXmlHandler extends DefaultHandler{
	private CurrentWeather currentWeather = new CurrentWeather();
	private ForecastWeather forecastWeather = new ForecastWeather();
	private boolean isCurrent_conditions = false;
	private boolean isForecast_conditions = false;
	private List<ForecastWeather> forecastList = new ArrayList<ForecastWeather>();
	public void endElement(String uri, String localName, String qName)
	throws SAXException {
		if(localName.equals("forecast_conditions")){
			forecastList.add(forecastWeather);
			isForecast_conditions = false;
		}else if (localName.equals("current_conditions")) {
			isCurrent_conditions = false;
		}
		super.endElement(uri, localName, qName);
	}
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if(localName.equals("current_conditions")){
			isCurrent_conditions = true;
		}else if(localName.equals("postal_code")){
			currentWeather.setCity(attributes.getValue("data"));
		}else if (localName.equals("current_date_time")) {
			currentWeather.setCurrentDate(attributes.getValue("data"));
		}else if (localName.equals("condition")) {
			if(isCurrent_conditions){
				currentWeather.setCondition(attributes.getValue("data"));
			}else {
				forecastWeather.setCondition(attributes.getValue("data"));
			}
		}else if (localName.equals("temp_c")) {
			currentWeather.setTemp(attributes.getValue("data")+"℃");
		}else if (localName.equals("humidity")) {
			currentWeather.setHumidity(attributes.getValue("data"));
		}else if (localName.equals("icon")) {
			if(isCurrent_conditions){
				String icon = attributes.getValue("data");
				String iconStr = icon.substring(icon.lastIndexOf("/")+1,icon.lastIndexOf(".")); 
				currentWeather.setIcon(iconStr);
			}else {
				String icon = attributes.getValue("data");
				String iconStr = icon.substring(icon.lastIndexOf("/")+1,icon.lastIndexOf(".")); 
				forecastWeather.setIcon(iconStr);
			}
		}else if (localName.equals("wind_condition")) {
			String wind_condition = attributes.getValue("data");
			String wind_direction = wind_condition.substring(0,wind_condition.indexOf("、"));
			String wind_speed = wind_condition.substring(wind_condition.indexOf("、")+1);
			currentWeather.setWind_direction(wind_direction);
			currentWeather.setWind_speed(wind_speed);
		}else if (localName.equals("forecast_conditions")){
			isForecast_conditions = true;
			forecastWeather = new ForecastWeather();
		}else if (localName.equals("day_of_week")) {
			forecastWeather.setDay_of_week(attributes.getValue("data"));
		}else if (localName.equals("low")) {
			forecastWeather.setLow_temp(attributes.getValue("data")+"℃");
		}else if (localName.equals("high")) {
			forecastWeather.setHigh_temp(attributes.getValue("data")+"℃");
		}
		super.startElement(uri, localName, qName, attributes);
	}
	public CurrentWeather getCurrentWeather(){
		return currentWeather;
	}
	public List<ForecastWeather> getForecastWeather(){
		return forecastList;
	}
}
